/**
 * 提交form检查，data是js的数组，其形式为：
 * var data = [{ruls:"验证规则", field:"验证字段1", msg:"错误信息提示"},
 *             {ruls:"验证规则", field:"验证字段1,验证字段2", msg:"错误信息提示"}];
 * 其中验证字段为表单元素的 #+ID，验证字段数量不限，用逗号分隔
 * 
 * 1、验证规则empty，判断验证字段值中的元素不能为空
 * 2、验证规则eq，判断验证字段值中的两个元素相等
 * 3、验证规则email，判断验证字段值中的元素值符合email书写规范
 * 4、验证规则int，判断验证字段值中的元素值必须是数字
 * 5、验证规则alpha，判断验证字段值中的元素值必须是英文字母
 * 6、验证规则alnum，判断验证字段值中的元素值必须是数字、英文字母或者下划线
 * 7、验证规则date，判断验证字段值中的元素值符合YYYY-MM-DD的日期格式(并不严格)
 */
(function ($) {
    var checkAlnum = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
    var checkNum = "0123456789";
    var checkAlpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    function isValid(element, msg, checkValid) {
        var checkValue = element.val();
        var isCheckPass = true;
        for (var i=0;i<checkValue.length;i++) {
            if (checkValid.indexOf(checkValue.substring(i, i+1)) >= 0) {
                continue;
            } else {
                isCheckPass = false;
                break;
            }
        }
        if(!isCheckPass) {
            alert(msg);
            element.select();
        }
        return isCheckPass;
    }

    function isEmail(element, msg) {
        var email = element.val();
        if (email != "" && email.search(/^[A-Za-z0-9]+([._\-\+]*[A-Za-z0-9]+)*@([A-Za-z0-9]+[-A-Za-z0-9]*[A-Za-z0-9]+\.)+[A-Za-z0-9]+$/) == -1) {
            alert(msg);
            element.select();
            return false;
        }
        return true;
    }

    function isEmpty(element, msg) {
        if ($.trim(element.val()) == "") {
            alert(msg);
            element.select();
            return false;
        }
        return true;
    }

    function isEqual(element1, element2, msg) {
        if ($.trim(element1.val()) != $.trim(element2.val())) {
            alert(msg);
            element2.select();
            return false;
        }
        return true;
    }

    function isDate(element, msg) {
        var date = element.val();
        if (date != "" && date.search(/^((19\d{2})|(20\d{2}))-((0[1-9]{1})|(1[0-2]{1}))-((0[1-9]{1})|([1-2]{1}[0-9]{1})|(3[0-1]{1}))$/) == -1) {
            alert(msg);
            element.select();
            return false;
        }
        return true;
    }

    var checkForm = function (data) {
        var checkResult = true;
        $.each(data, function(num, info) {
            switch (info.rule) {
                case "empty" :
                    checkResult = isEmpty($(info.field), info.msg);
                    break;
                case "eq" :
                    var fileds = info.field.split(",");
                    checkResult = isEqual($(fileds[0]), $(fileds[1]), info.msg);
                    break;
                case "email" :
                    checkResult = isEmail($(info.field), info.msg);
                    break;
                case "int" :
                    checkResult = isValid($(info.field), info.msg, checkNum);
                    break;
                case "alpha" :
                    checkResult = isValid($(info.field), info.msg, checkAlpha);
                    break;
                case "alnum" :
                    checkResult = isValid($(info.field), info.msg, checkAlnum);
                    break;
                case "date" :
                    checkResult = isDate($(info.field), info.msg);
                    break;
                default :
                    checkResult = true;
            }
            if (!checkResult) {
                return false;
            }
        });
        return checkResult;
    };

    $.extend({checkform:checkForm});
})(jQuery);